package models

import (
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"time"
)

// 呼叫记录表
type CallRecord struct {
	Id            int64
	CompanyId     int64     `orm:"description(被叫所属企业id)"`
	MobileId      int64     `orm:"description(被叫id)"`
	TaskId        int64     `orm:"description(被叫所属任务id)"`
	ServiceId     int64     `orm:"description(所属座席id)"`
	CallId        string    `orm:"description(呼叫id)"`
	Caller        string    `orm:"description(主叫/线路标识)"`
	Called        string    `orm:"description(被叫号码)"`
	RecordUrl     string    `orm:"description(录音地址)"`
	BillDuration  int       `orm:"description(账单计费时长/min)"`
	CallDuration  int       `orm:"description(呼叫时长/s)"`
	AgentDuration int       `orm:"description(座席介入时长/s)"`
	StartTime     time.Time `orm:"null;description(呼叫开始时间)"`
	AnswerTime    time.Time `orm:"null;description(接听时间时间)"`
	TransferTime  time.Time `orm:"null;description(转接时间)"`
	EndTime       time.Time `orm:"null;description(呼叫结束时间)"`
	Intention     int32     `orm:"null;description(意向分类)"`
	MarketResult  int32     `orm:"null;description(营销结果)"`
	BasicModel
}

// 营销结果
const (
	MarketUnknown = iota + 1
	MarketFailed
	MarketSuccess
)

type RecordTextDetail struct {
	Id          int64
	Idx         int    `orm:"description(序号)`
	CallId      string `orm:"description(呼叫id)"`
	Type        string `orm:"description(文本类型 PERSON: 客户 ROBOT: 机器人文本)"`
	Text        string `orm:"description(语音文本)"`
	StartOffset int    `orm:"description(录音起始偏移量)"`
	EndOffset   int    `orm:"description(录音结束偏移量)"`
}

func InsertRecordDetailsToDb(records []RecordTextDetail) bool {

	num := len(records)
	if 0 == num || nil == records {
		return false
	}

	o := orm.NewOrm()
	o.Using("default")

	succNum, err := o.InsertMulti(num, records)
	if err != nil || num != int(succNum) {
		logs.Error("insert to db failed :%v,success:%v,want to:%v!", err, succNum, num)
		return false
	}

	return true
}

func GetRecordsByCallId(callId string) (records []RecordTextDetail, err error) {

	o := orm.NewOrm()
	o.Using("default")

	_, err = o.QueryTable(new(RecordTextDetail)).Filter("CallId", callId).All(&records)

	return
}
